<script setup lang="ts">
// import { getUserInfo } from '@/apis/login'
import { getUserInfo } from '@/apis/login'
import router from '@/router'

import type { LoginParamsApI } from '@/types/user'
import { removeLocalToken } from '@/utils/auth'

import { Unlock, Document } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
// 后台返回用户信息
const userData = ref<LoginParamsApI>({
  id: '',
  name: '',
  photo: '',
  mobile: '',
  gender: '',
  birthday: '',
})

// 发送请求获取用户信息
const userInfo = async () => {
  // 获取token

  const res = await getUserInfo()
  // console.log('res:', res)
  // 将后台返回的数据存入表格
  userData.value = res.data
}
userInfo()

// 退出登录
const onLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '温馨提醒', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // 1.删除token
      removeLocalToken()
      // 2.跳转至登录页
      router.push('/login')
      // 3.弹窗提醒
      ElMessage({
        type: 'success',
        message: '退出成功！',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消成功！',
      })
    })
}
</script>

<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <!-- logo -->
      <div class="logo"></div>
      <!-- 导航菜单容器 el-menu -->
      <el-menu text-color="#fff" background-color="#023" :default-active="$route.path" router>
        <el-menu-item index="/" :icon="Document">数据概览</el-menu-item>
        <el-menu-item index="/article">内容管理</el-menu-item>
        <el-menu-item index="/publish">发布文章</el-menu-item>
        <el-menu-item index="/user">个人中心</el-menu-item>
      </el-menu>
      <!-- 导航栏 -->
    </el-aside>

    <el-container>
      <el-header>
        <span>{{ userData.name }}</span>
        <el-link :icon="Unlock" :underline="false" @click="onLogout()">退出</el-link>
      </el-header>

      <el-main>
        <!-- 路由出口 -->
        <routerView />
      </el-main>
    </el-container>
  </el-container>
</template>
<style scoped lang="scss">
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #023;

    .logo {
      width: 200px;
      height: 60px;
      background: #023 url('@/assets/images/logo.png') no-repeat center / 160px auto;
    }
  }

  .el-header {
    border-bottom: 1px solid #000000;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
    padding-right: 15px;
  }
}
* {
  margin: 0;
  padding: 0;
  border: 0;
}
</style>
